package demo02;

import java.util.concurrent.ThreadFactory;

public class ThreadExceptionUtils {
	static class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
		// 处理从线程里抛出来的异常。
		public void uncaughtException(Thread t, Throwable e) {
//			 System.out.println("Catched Throwable: " +
//			 e.getClass().getSimpleName() + ", " + e.getMessage());
//	        System.out.printf("An exception has been captured\n");  
//	        System.out.printf("Thread: %s\n", t.getId());  
//	        System.out.printf("Exception: %s: %s\n",   
//	                e.getClass().getName(), e.getMessage());  
//	        System.out.printf("Stack Trace: \n");  
//	        e.printStackTrace(System.out);  
//	        System.out.printf("Thread status: %s\n", t.getState());  
			System.out.println("zhaoyan:" + e.getMessage());
		}
	}

	static class MyThreadFactory implements ThreadFactory {
		// 重新组织创建线程的方式
		public Thread newThread(Runnable r) {
			Thread t = new Thread(r);
			// 为每一个线程都绑定一个异常处理器。
			t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
			return t;
		}
	}

	public static MyThreadFactory getMyThreadFactory() {
		return new MyThreadFactory();
	}
}
